home *** CD-ROM | disk | FTP | other *** search
/ 45 Great Windows Utilities 7 / 45 Great Windows Utilities Volume 7 MOJO-411 (Mojo Software).iso / cdeject / cdeject.c next >
C/C++ Source or Header  |  1993-11-18  |  2KB  |  73 lines

  1. #include <windows.h>
  2. #include <mmsystem.h>
  3.  
  4. /*
  5.  
  6. CDEJECT
  7.  
  8. Run it, and it ejects the CD from the first CD-ROM drive, if
  9. it can.  If you run it with the control key down, it prints
  10. the about box (no big whoop).
  11.  
  12. This program is Copyright 1993 by David C. Elliott.   It's freeware,
  13. not shareware, and anyone can use or distribute it as long as they
  14. don't sell it.
  15.  
  16. */
  17.  
  18.  
  19. static void
  20. about()
  21. {
  22.     MessageBox(NULL, "CD Eject\nCopyright 1993 David C. Elliott", "About CD Eject", MB_OK);
  23. }
  24.  
  25. int PASCAL WinMain(HANDLE hInstance,
  26.            HANDLE hPrevInstance,
  27.            LPSTR  lpszCmdLine,
  28.            int    nCmdShow)
  29. {
  30.     MCI_OPEN_PARMS op;
  31.     MCI_SET_PARMS mciset;
  32.     DWORD mciErr;
  33.     char ebuf[100];
  34.     int printErrors = 1;
  35.  
  36.     if (lpszCmdLine[0] == '-' && (lpszCmdLine[1] == 'q' || lpszCmdLine[1] == 'Q')) {
  37.         printErrors = 0;
  38.     }
  39.     
  40.     if (GetKeyState(VK_CONTROL) & 0x8000) {
  41.         about();
  42.         return 0;
  43.     }
  44.  
  45.     if (GetKeyState(VK_SPACE) & 0x8000) {
  46.         printErrors = 1;
  47.     }
  48.     
  49.     op.lpstrDeviceType = "cdaudio";
  50.     
  51.     mciErr = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_WAIT, (LONG)&op);
  52.     if (mciErr != 0) {
  53.         if (printErrors) {
  54.             mciGetErrorString(mciErr, (LPSTR)ebuf, sizeof(ebuf) - 1);
  55.             MessageBox(NULL, ebuf, "CD-ROM error", MB_OK);
  56.         }
  57.         return 0;
  58.     }
  59.     
  60.     mciErr = mciSendCommand(op.wDeviceID, MCI_SET, (DWORD)MCI_SET_DOOR_OPEN,(DWORD)&mciset);
  61.     if (mciErr != 0) {
  62.         if (printErrors) {
  63.             mciGetErrorString(mciErr, (LPSTR)ebuf, sizeof(ebuf) - 1);
  64.             MessageBox(NULL, ebuf, "CD-ROM error", MB_OK);
  65.         }
  66.         return 0;
  67.     }
  68.     
  69.     return 0;
  70.  
  71. } /*  End of WinMain                                                    */
  72.  
  73.